home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // DlgDemo5.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include "Resource.h"
- #include "DlgDemo5.h"
-
- CMyApp myApp;
-
- /////////////////////////////////////////////////////////////////////////
- // CMyApp member functions
-
- BOOL CMyApp::InitInstance ()
- {
- m_pMainWnd = new CMainWindow;
- m_pMainWnd->ShowWindow (m_nCmdShow);
- m_pMainWnd->UpdateWindow ();
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMainWindow message map and member functions
-
- BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
- ON_WM_ERASEBKGND ()
- ON_WM_PAINT ()
- ON_COMMAND (IDM_OPTIONS_SETTINGS, OnOptionsSettings)
- ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
- ON_COMMAND (IDM_OPTIONS_ABOUT, OnOptionsAbout)
- ON_MESSAGE (WM_USER, OnApplySettings)
- END_MESSAGE_MAP ()
-
- CMainWindow::CMainWindow ()
- {
- m_pDlg = NULL;
-
- m_nFillType = 1;
- m_nFillColor = 2;
- m_text = "Hello, MFC";
- m_nHeight = 72;
- m_bBold = TRUE;
- m_bItalic = TRUE;
-
- Create (NULL, "DlgDemo5", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
- MAKEINTRESOURCE (IDR_MAINFRAME));
- }
-
- BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
- {
- CRect rect;
- GetClientRect (&rect);
-
- m_nFillType == 1 ? DoGradientFill (pDC, &rect) :
- DoSolidFill (pDC, &rect);
- return TRUE;
- }
-
- void CMainWindow::OnPaint ()
- {
- CRect rect;
- GetClientRect (&rect);
-
- CPaintDC dc (this);
- DoDrawText (&dc, &rect);
- }
-
- void CMainWindow::OnOptionsSettings ()
- {
- if (m_pDlg != NULL) {
- m_pDlg->SetFocus ();
- return;
- }
-
- m_pDlg = new CSettingsDialog;
-
- m_pDlg->m_nFillType = m_nFillType;
- m_pDlg->m_nFillColor = m_nFillColor;
- m_pDlg->m_text = m_text;
- m_pDlg->m_nHeight = m_nHeight;
- m_pDlg->m_bBold = m_bBold;
- m_pDlg->m_bItalic = m_bItalic;
-
- m_pDlg->Create (IDD_SETTINGSDLG, this);
- }
-
- void CMainWindow::OnOptionsExit ()
- {
- SendMessage (WM_CLOSE, 0, 0);
- }
-
- void CMainWindow::OnOptionsAbout ()
- {
- CAboutDialog dlg (this);
- dlg.DoModal ();
- }
-
- LONG CMainWindow::OnApplySettings (UINT wParam, LONG lParam)
- {
- PPROPERTIES pProp = (PPROPERTIES) lParam;
-
- m_nFillType = pProp->nFillType;
- m_nFillColor = pProp->nFillColor;
- m_text = pProp->text;
- m_nHeight = pProp->nHeight;
- m_bBold = pProp->bBold;
- m_bItalic = pProp->bItalic;
-
- Invalidate ();
- return 0;
- }
-
- void CMainWindow::DoSolidFill (CDC* pDC, CRect* pRect)
- {
- static COLORREF crColor[5] = {
- RGB (255, 0, 0),
- RGB ( 0, 255, 0),
- RGB ( 0, 0, 255),
- RGB (255, 0, 255),
- RGB ( 0, 255, 255),
- };
-
- CBrush brush (crColor[m_nFillColor]);
- pDC->FillRect (pRect, &brush);
- }
-
- void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
- {
- CBrush* pBrush[64];
- for (int i=0; i<64; i++) {
- switch (m_nFillColor) {
-
- case 0:
- pBrush[i] = new CBrush (RGB (255 - (i * 4), 0, 0));
- break;
-
- case 1:
- pBrush[i] = new CBrush (RGB (0, 255 - (i * 4), 0));
- break;
-
- case 2:
- pBrush[i] = new CBrush (RGB (0, 0, 255 - (i * 4)));
- break;
-
- case 3:
- pBrush[i] = new CBrush (RGB (255 - (i * 4), 0,
- 255 - (i * 4)));
- break;
-
- case 4:
- pBrush[i] = new CBrush (RGB (0, 255 - (i * 4),
- 255 - (i * 4)));
- break;
- }
- }
-
- int nWidth = pRect->Width ();
- int nHeight = pRect->Height ();
- CRect rect;
-
- for (i=0; i<nHeight; i++) {
- rect.SetRect (0, i, nWidth, i + 1);
- pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
- }
-
- for (i=0; i<64; i++)
- delete pBrush[i];
- }
-
- void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
- {
- CFont font;
- int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * m_nHeight) / 72);
-
- font.CreateFont (nHeight, 0, 0, 0, m_bBold ? FW_BOLD : FW_NORMAL,
- m_bItalic, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
- CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
- FF_DONTCARE, "Times New Roman");
-
- pDC->SetBkMode (TRANSPARENT);
- pDC->SetTextColor (RGB (255, 255, 255));
-
- CFont* pOldFont = pDC->SelectObject (&font);
- pDC->DrawText (m_text, -1, pRect, DT_SINGLELINE | DT_CENTER |
- DT_VCENTER);
-
- pDC->SelectObject (pOldFont);
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CSettingsDialog message map and member functions
-
- BEGIN_MESSAGE_MAP (CSettingsDialog, CDialog)
- ON_BN_CLICKED (IDC_DEFAULTS, OnDefaults)
- END_MESSAGE_MAP ()
-
- void CSettingsDialog::OnOK ()
- {
- UpdateData (TRUE);
-
- PROPERTIES prop;
- prop.nFillType = m_nFillType;
- prop.nFillColor = m_nFillColor;
- prop.text = m_text;
- prop.nHeight = m_nHeight;
- prop.bBold = m_bBold;
- prop.bItalic = m_bItalic;
-
- AfxGetMainWnd ()->SendMessage (WM_USER, 0, (LPARAM) &prop);
- }
-
- void CSettingsDialog::OnCancel ()
- {
- DestroyWindow ();
- }
-
- void CSettingsDialog::PostNcDestroy ()
- {
- ((CMainWindow*) AfxGetMainWnd ())->m_pDlg = NULL;
- delete this;
- }
-
- void CSettingsDialog::OnDefaults ()
- {
- m_nFillType = 1;
- m_nFillColor = 2;
- m_text = "Hello, MFC";
- m_nHeight = 72;
- m_bBold = TRUE;
- m_bItalic = TRUE;
-
- UpdateData (FALSE);
- }
-
- void CSettingsDialog::DoDataExchange (CDataExchange* pDX)
- {
- CDialog::DoDataExchange (pDX);
-
- DDX_Radio (pDX, IDC_SOLID, m_nFillType);
- DDX_Radio (pDX, IDC_RED, m_nFillColor);
- DDX_Text (pDX, IDC_TEXT, m_text);
- DDX_Text (pDX, IDC_HEIGHT, m_nHeight);
- DDV_MinMaxInt (pDX, m_nHeight, 8, 144);
- DDX_Check (pDX, IDC_BOLD, m_bBold);
- DDX_Check (pDX, IDC_ITALIC, m_bItalic);
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CAboutDialog message map and member functions
-
- BEGIN_MESSAGE_MAP (CAboutDialog, CDialog)
- ON_WM_PAINT ()
- END_MESSAGE_MAP ()
-
- BOOL CAboutDialog::OnInitDialog ()
- {
- CDialog::OnInitDialog ();
-
- CStatic* pStatic = (CStatic*) GetDlgItem (IDC_ICONRECT);
- pStatic->GetWindowRect (&m_rect);
- pStatic->DestroyWindow ();
- ScreenToClient (&m_rect);
-
- return TRUE;
- }
-
- void CAboutDialog::OnPaint ()
- {
- CPaintDC dc (this);
- HICON hIcon = (HICON) ::GetClassLong (AfxGetMainWnd ()->m_hWnd,
- GCL_HICON);
-
- if (hIcon != NULL) {
- CDC dcMem;
- dcMem.CreateCompatibleDC (&dc);
-
- CBitmap bitmap;
- bitmap.CreateCompatibleBitmap (&dc, 32, 32);
- CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
-
- CBrush brush (::GetSysColor (COLOR_3DFACE));
- dcMem.FillRect (CRect (0, 0, 32, 32), &brush);
- dcMem.DrawIcon (0, 0, hIcon);
-
- dc.StretchBlt (m_rect.left, m_rect.top, m_rect.Width(),
- m_rect.Height (), &dcMem, 0, 0, 32, 32, SRCCOPY);
-
- dcMem.SelectObject (pOldBitmap);
- }
- }
-